package firstPack;

import java.util.ArrayList;
import java.util.Random;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

/**
 * Reached though the main menu, it runs the actual game. It creates,
 * controls, and deletes all necessary objects. It can lead to the
 * pause state and the Game over state.
 * State 1
 * @author El Jefe
 *
 */
public class PlayGameState extends BasicGameState{
	
	private ArrayList<Circle> balls;
	private Circle mouseBall;
	private int timePassed;
	private int timeIncreaseDiff;
	private Random rand;

	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {	
		balls = new ArrayList<Circle>();
		mouseBall = new Circle(0,0,20);
		timePassed = 0;
		timeIncreaseDiff = 0;
		rand = new Random();
		GameState.totalScore = 0;
		GameState.lives = 5;
		
	}
	
	public void enter(GameContainer container, StateBasedGame game){
		if (GameState.lives <= 0){
			balls = new ArrayList<Circle>();
			GameState.totalScore = 0;
			GameState.lives = 5;
		}
	}

	@Override
	public void render(GameContainer container, StateBasedGame game, Graphics disp)
			throws SlickException {
		disp.drawString("You are playing the game! : " + timeIncreaseDiff, 30, 30);
		if (timeIncreaseDiff > 15000){
			disp.drawString("Difficulty + ", 30, 45);
		}
		disp.setColor(Color.yellow);
		disp.fill(mouseBall);
		disp.setColor(Color.white);
		
		disp.setColor(Color.red);
		for(Circle c: balls){
			disp.fill(c);
		}
		disp.setColor(Color.white);
		disp.drawString("Score : " + GameState.totalScore + " ", 900, 30); //+ balls.size()
		disp.drawString("Lives : " + GameState.lives, 900, 45);
	}

	@Override
	public void update(GameContainer container, StateBasedGame sbg, int delta)
			throws SlickException {
		Input input = container.getInput();
		if(input.isKeyPressed(input.KEY_SPACE)){
			sbg.enterState(2);
		}
		mouseBall.setCenterX(container.getInput().getMouseX());
		mouseBall.setCenterY(container.getInput().getMouseY());
		
		timePassed += delta;
		timeIncreaseDiff += delta;
		if(timePassed > 300){
			timePassed = 0;
			balls.add(new Circle(12 + rand.nextInt(1001),0,10));
			if (timeIncreaseDiff > 20000){
				balls.add(new Circle(12 + rand.nextInt(1001),0,10));
			}
		}
		
		for(Circle c : balls){
//			c.setCenterX(c.getCenterX() - 1);
			c.setCenterY(c.getCenterY() + delta/(float)(1 + rand.nextInt(10)));
			
		}
		
			
			for(int i = balls.size() - 1; i >= 0; i--){
				Circle c = balls.get(i);
				if(c.getCenterY() > 770){
					balls.remove(i);
					GameState.lives -= 1;
				}
				if(c.intersects(mouseBall)){
					balls.remove(i);
					GameState.totalScore += 1;
				}
			}
			
			if(GameState.lives <= 0){
				timeIncreaseDiff = 0;
				input.clearKeyPressedRecord();
				sbg.enterState(3, new FadeOutTransition(), new FadeInTransition());
			}
		
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return 1;
	}
	

}
